﻿using System.Collections.Generic;
using Entitas;
/// <summary>
/// once the player has attacked the attack timer must cool down before the player can attack again.
/// once attack timer reaches 0 it is removed from the player. 
/// </summary>
class PlayerAttackTimerSystem : ReactiveSystem<PlayerEntity>, ICleanupSystem
{
    PlayerContext playerContext;
    public PlayerAttackTimerSystem(Contexts context) : base(context.player)
    {
        playerContext = context.player;
    }
    protected override void Execute(List<PlayerEntity> entities)
    {
        foreach (var e in entities)
        {
            e.ReplaceAttackTimer(e.attackTimer.value - .06f);
        }
    }

    protected override bool Filter(PlayerEntity entity)
    {
        return entity.hasAttackTimer;
    }

    protected override ICollector<PlayerEntity> GetTrigger(IContext<PlayerEntity> context)
    {
        return context.CreateCollector(PlayerMatcher.AttackTimer.AddedOrRemoved());
    }

    public void Cleanup()
    {
        if (playerContext.count == 1)
        {
            var playerEntity = playerContext.GetEntities().SingleEntity<PlayerEntity>();

            if (playerEntity.hasAttackTimer)
            {
                if (playerEntity.attackTimer.value <= 0)
                {
                    playerEntity.RemoveAttackTimer();
                    //player can now initate another attack because attacking boolean is now false
                    playerEntity.isAttacking = false;
                }
            }
        }
    }
}


